#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e3 + 10;
long long dp[MAXN][2][2][MAXN];
long long a[MAXN];
void doit()
{
    memset(dp, 0, sizeof(dp));
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }
    for (int i = 2; i <= n; ++i)
    {
        if (i == 2)
        {
            dp[2][0][0][0] = (a[1] == a[2] ? a[2] : 0);
            dp[2][1][0][0] = 0;
            dp[2][0][1][0] = 0;
            dp[2][1][1][0] = (a[1] == a[2] ? a[2] : 0);
            continue;
        }
        if (i == 3)
        {
            dp[3][0][0][0] = dp[2][0][0][0] + (a[2] == a[3] ? a[3] : 0);
            dp[3][1][0][0] = dp[2][0][0][0];
            dp[3][0][1][0] = dp[2][1][1][0];
            dp[3][1][1][0] = dp[2][1][1][0] + (a[2] == a[3] ? a[3] : 0);
            dp[3][0][0][1] = (a[2] == a[3] ? a[3] : 0);
            dp[3][0][1][1] = (a[1] == a[3] ? a[3] : 0);
            dp[3][1][1][1] = (a[2] == a[3] ? a[3] : 0);
            dp[3][1][0][1] = (a[1] == a[3] ? a[3] : 0);
            continue;
        }
        for (int j = 0; j <= i - 2; ++j)
        {
            if (j == 0)
            {
                dp[i][0][0][0] = dp[i - 1][0][0][0] + (a[i - 1] == a[i] ? a[i] : 0);
                dp[i][0][1][0] = dp[i - 1][1][1][0];
                dp[i][1][0][0] = dp[i - 1][0][0][0];
                dp[i][1][1][0] = dp[i - 1][1][1][0] + (a[i - 1] == a[i] ? a[i] : 0);
            }
            else
            {
                if (j == i - 2)
                {
                    for (int k = 0; k <= i - 3; ++k)
                    {
                        dp[i][0][0][j] = max(dp[i][0][0][j], dp[i - 1][0][1][k] + (a[i - 1] == a[i] ? a[i] : 0));
                    }
                }
                else
                {
                    dp[i][0][0][j] = dp[i - 1][0][0][j] + (a[i - 1] == a[i] ? a[i] : 0);
                }
                if (j == i - 2)
                {
                    for (int k = 0; k <= i - 3; ++k)
                    {
                        dp[i][0][1][j] = max(dp[i][0][1][j], dp[i - 1][1][0][k] + (a[i - 2] == a[i] ? a[i] : 0));
                    }
                }
                else
                {
                    dp[i][0][1][j] = dp[i - 1][1][1][j] + (a[j] == a[i] ? a[i] : 0);
                }
                if (j == i - 2)
                {
                    for (int k = 0; k <= i - 3; ++k)
                    {
                        dp[i][1][0][j] = max(dp[i][1][0][j], dp[i - 1][0][1][k] + (a[i - 2] == a[i] ? a[i] : 0));
                    }
                }
                else
                {
                    dp[i][1][0][j] = dp[i - 1][0][0][j] + (a[j] == a[i] ? a[i] : 0);
                }
                if (j == i - 2)
                {
                    for (int k = 0; k <= i - 3; ++k)
                    {
                        dp[i][1][1][j] = max(dp[i][1][1][j], dp[i - 1][1][0][k] + (a[i - 1] == a[i] ? a[i] : 0));
                    }
                }
                else
                {
                    dp[i][1][1][j] = dp[i - 1][1][1][j] + (a[i - 1] == a[i] ? a[i] : 0);
                }
            }
        }
    }
    long long ansmax = 0;
    for (int urt = 0; urt <= 1; ++urt)
    {
        for (int vhc = 0; vhc <= 1; ++vhc)
        {
            for (int p = 0; p <= n - 2; ++p)
            {
                ansmax = max(ansmax, dp[n][urt][vhc][p]);
            }
        }
    }
    cout << ansmax << endl;
}

int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;
    cin >> T;
    for (int p = 1; p <= T; ++p)
    {
        doit();
    }
    return 0;
}
